package com.example.service;

import com.example.VO.BaseResultVO;
import com.example.VO.UserVO;
import com.example.mapper.UserMapper;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
 * Created by ykbboom on 2017/3/3.
 */
@Service("userService")
public class UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 新添加用户
     * */
    @Transactional
    public void addUser(User user){
         userMapper.addUser(user);
    }
    /**
     * 更新用户密码
     */
    @Transactional
    public BaseResultVO updateUserPass(String userPhone,String newPass){
        userMapper.updateUserPassword(newPass,userPhone);
        return new BaseResultVO(null,BaseResultVO.RESULT_CODE_OK,"更新密码成功。");
    }
    /**
     * 获取用户Id
     * */
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public Integer getUserId(String userPhone){
        return userMapper.checkUserExists(userPhone);
    }
    /**
     * 检查用户是否存在,false代表不存在,true代表存在
     * */
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public boolean checkUserExists(String userPhone){
        return userMapper.checkUserExists(userPhone)==null?false:true;
    }
    /**
     * 验证用户登录
     * */
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public User validateUser(String userPhone,String userPass){
        User user = userMapper.getUserInfoByUserPhone(userPhone);
        if (user==null){
            return null;
        }else if (!user.getUserPass().equals(userPass)){
            return null;
        }
        return user;
    }
    /**
     * 查看用户积分
     * */
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public UserVO checkUserPoint(String userPhone){
        Integer point = userMapper.getUserPointByUserPhone(userPhone);
        if (point==null){
            return null;
        }else {
            UserVO userVO = new UserVO();
            userVO.setResultCode(BaseResultVO.RESULT_CODE_OK);
            userVO.setUserPoint(point);
            return userVO;
        }
    }
}
